package suites.logger {
	import org.flexunit.asserts.assertEquals;
	import org.flexunit.asserts.assertFalse;
	import org.flexunit.asserts.assertNotNull;
	import org.flexunit.asserts.assertTrue;
	import org.splink.library.logging.ILogger;
	import org.splink.library.logging.ILoggerFactory;
	import org.splink.library.logging.LoggerFactory;
	import org.splink.library.logging.LoggerProvider;
	import org.splink.library.logging.NullLogger;

	/**
	 * @author maxmc
	 */
	public class BasicLoggerTest {
		private static var _logs : Array;
		private static var _factory : ILoggerFactory;

		[BeforeClass]
		public static function beforeClass() : void {
			_factory = new LoggerFactory("test");
			_factory.addLogOutput(new TestOutput(_logs = new Array()));
			assertEquals("test", _factory.id);

			LoggerProvider.addLoggerFactory(_factory);
		}
		
		[AfterClass]
		public static function afterClass() : void {
			LoggerProvider.removeLoggerFactory(_factory);
		}

		[Before]
		public function before() : void {
			_logs.splice(0, _logs.length);
		}

		private function getLogger() : ILogger {
			return LoggerProvider.getLogger("test", BasicLoggerTest);
		}

		[Test]
		public function testGetLogger() : void {
			var logger : ILogger = getLogger();
			assertNotNull(logger);
			assertFalse(logger is NullLogger);
		}

		[Test]
		public function testFinalizeLogger() : void {
			var logger : ILogger = getLogger();
			logger.finalize();
		}

		[Test]
		public function testGetLogoutput() : void {
			assertNotNull(getLogger().getLogOutput(TestOutput));
		}

		[Test]
		public function testTrace() : void {
			getLogger().trace("trace");
			assertEquals(1, _logs.length);
			assertTrue(String(_logs[0]).indexOf("suites.logger.BasicLoggerTest.testTrace") > -1);
			assertTrue(String(_logs[0]).indexOf("TRACE trace") > -1);
		}

		[Test]
		public function testInfo() : void {
			getLogger().info("info");
			assertEquals(1, _logs.length);
			assertTrue(String(_logs[0]).indexOf("suites.logger.BasicLoggerTest.testInfo") > -1);
			assertTrue(String(_logs[0]).indexOf("INFO info") > -1);
		}

		[Test]
		public function testDebug() : void {
			getLogger().debug("debug");
			assertEquals(1, _logs.length);
			assertTrue(String(_logs[0]).indexOf("suites.logger.BasicLoggerTest.testDebug") > -1);
			assertTrue(String(_logs[0]).indexOf("DEBUG debug") > -1);
		}

		[Test]
		public function testWarn() : void {
			getLogger().warn("warn");
			assertEquals(1, _logs.length);
			assertTrue(String(_logs[0]).indexOf("suites.logger.BasicLoggerTest.testWarn") > -1);
			assertTrue(String(_logs[0]).indexOf("WARN warn") > -1);
		}

		[Test]
		public function testError() : void {
			getLogger().error("error");
			assertEquals(1, _logs.length);
			assertTrue(String(_logs[0]).indexOf("suites.logger.BasicLoggerTest.testError") > -1);
			assertTrue(String(_logs[0]).indexOf("ERROR error") > -1);
		}

		[Test]
		public function testFatal() : void {
			getLogger().fatal("fatal");
			assertEquals(1, _logs.length);
			assertTrue(String(_logs[0]).indexOf("suites.logger.BasicLoggerTest.testFatal") > -1);
			assertTrue(String(_logs[0]).indexOf("FATAL fatal") > -1);
		}
	}
}
import org.splink.library.logging.LogEvent;
import org.splink.library.logging.logoutput.ILogOutput;
import org.splink.library.logging.logoutput.IOutputFormatter;

internal class TestOutput implements ILogOutput {
	private var _formatter : IOutputFormatter;
	private var _logs : Array;

	public function TestOutput(logs : Array) {
		_logs = logs;
	}

	public function setOuputFormatter(formatter : IOutputFormatter) : void {
		_formatter = formatter;
	}

	public function getInitialized() : Boolean {
		return true;
	}

	public function log(event : LogEvent) : void {
		_logs.push(_formatter.format(event));
	}
}